package com.example.rouroudate.ui.gallery.ui

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.ListView
import com.example.rouroudate.R
import com.example.rouroudate.ui.widget.*
import com.example.rouroudate.util.DisplayUtils
import com.example.rouroudate.util.PrefManager
import com.example.rouroudate.util.TimeUtils
import com.example.rouroudate.util.ToastUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class DateSettingDialog constructor(context: Context) : CommonBottomDialog(context) {

    companion object {
        const val TAG = "DateSettingDialog"
        private const val DateSize = 70
        private const val HurtSize = 15
    }

    private lateinit var dateListView: ListView
    private lateinit var dayListView: ListView

    private val selectedData = PrefManager.getDateSetting()

    private val dateData = ArrayList<String>().apply {
        for (i in 15..DateSize) {
            add(String.format("%s 天", i))
        }
    }

    private val hurtData = ArrayList<String>().apply {
        for (i in 1..HurtSize) {
            add(String.format("%s 天", i))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.date_setting_layout)
        dateListView = findViewById(R.id.date_view)!!
        dayListView = findViewById(R.id.day_view)!!
        initAdapter(dateListView, dateData, TimeUtils.MONTH)
        initAdapter(dayListView, hurtData, TimeUtils.DAY)
        setRightViewClick{
            ToastUtils.show(if (PrefManager.setDateSetting(selectedData)) {
                GlobalScope.launch {
                    DataEventBus.dateSettingData.emit(selectedData)
                }
                context.getString(R.string.edit_success)
            } else {
                context.getString(R.string.edit_fail)
            })
            dismiss()
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initAdapter(view: ListView, data: ArrayList<String>, type: Int) {
        view.setOnTouchListener { v, event ->
            Log.d(TAG, "TouchListener ${event.action}")
            when(event.action) {
                MotionEvent.ACTION_DOWN -> v.isNestedScrollingEnabled = true
                MotionEvent.ACTION_UP -> v.isNestedScrollingEnabled = false
                else -> {}
            }
            false
        }
        view.adapter = DateListAdapter(data, context)
        smooth(view, type, data)
        view.setOnScrollListener(DateScrollListener(dateScrollCallback, type))
    }

    private val dateScrollCallback = object : DateScrollCallback {
        override fun onScrollEnd(position: Int, type: Int) {
            when(type) {
                TimeUtils.MONTH -> selectedData.dateSize = position % dateData.size + 15
                else -> selectedData.hurtSize = position % HurtSize + 1
            }
        }
    }

    private fun smooth(p0: ListView, type: Int, data: ArrayList<String>) {
        val h = (DisplayUtils.getScreenHeight() * 0.3f).toInt()
        val height = DisplayUtils.dp2px(85f)
        val count = h / height + if (h / height == 0) 0 else 1
        val offset = ((h - height) / 2) % height - height
        val midZero = Int.MAX_VALUE / 2 - (Int.MAX_VALUE / 2 % data.size)
        val offsetNum = 1 + count / 2
        val position = when(type) {
            TimeUtils.MONTH -> midZero + selectedData.dateSize - 14 - offsetNum
            else -> midZero + selectedData.hurtSize - offsetNum
        }
        p0.setSelectionFromTop(position, offset)
    }
}